#===============================================================================
#  ns tracefile parsing
#===============================================================================
import re

# activity by nodes
nodes = [[],[],[],[],[],[],[],[]]
# map to trace a single package
packages = {} 

aodv_packages = []
#packets stats 
results = {}

#event class
class inf:
    pass

# actions on event
def work(s):
    i = inf()
    i.event_type = s[0]
    if i.event_type == 'M' :
        return
 
    i.time = float(re.search(r'-t (.*?) ', s).group(1))
    i.node = int(re.search(r'-Ni (.*?) ', s).group(1))
    i.type = re.search(r'-It (.*?) ', s).group(1)
    i.len = int(re.search(r'-Il (.*?) ', s).group(1))
    i.nl = re.search(r'-Nl (.*?) ', s).group(1)
    s = re.search(r'-Ii (.*?) ', s)
    if i.type == "AODV":
        aodv_packages.append(i)
        return 
    
    if s :
        i.id = int(s.group(1))
        if i.id in packages.keys() :
            packages[i.id].append(i)
            # filter fist send and receive at node 0 
            if len(packages[i.id]) > 2 :
                nodes[i.node].append(i) 
        else :
            packages[i.id]= [i]

                


# file
f = open("simple.tr","r")
# no of items to read 
#i = 20000
for s in f:
    work(s);
#    i = i-1
#    if i<0 :
#        break


for pack in packages.itervalues():
    del pack[0:2]
    start = pack[0]
    end = pack[-1]
    for p  in pack:
        p.start = start
        p.end = end
    

# print history of single package  
for id, pack in packages.iteritems():
    timespend = pack[-1].time - pack[0].time
    tp = pack[-1].event_type+`pack[-1].node`
    if tp in results.keys():
        results[tp].append(pack)
    else:
        results[tp] = [pack]
#    print id ,pack[0].type,timespend,":", 
    
#    for package_event in pack :
#        print package_event.event_type, package_event.node,"@",package_event.time,"|",
#    print 

for id, amount in results.iteritems():
    print id, len(amount)

print

# time period for stats 
dct=1

# print load be node
for i,nodestat in zip(range(0,len(nodes)), nodes):
    print i
    ct=dct
    
    csv = open(`i`+".csv","w")
    # amount of sent, received, forwarded, and dropped bytes
    send,recv,fwd,drp=0,0,0,0
    for node_event in nodestat:
        # print stats for next period 
        if node_event.time > ct :
            print '@ ct',ct,'\tsend:' , send*8/(dct*1024), '\trecv:', recv*8/(dct*1024),'\tfwd:',fwd*8/(dct*1024),'\tdrop:',drp*8/(dct*1024)
            csv.write(`ct`+"\t"+`send*8/(dct*1024)`+"\t"+`recv*8/(dct*1024)`+"\t"+`fwd*8/(dct*1024)`+"\t"+`drp*8/(dct*1024)`+"\n")
            send,recv,fwd,drp=0,0,0,0
            ct+= dct
        if node_event.event_type == 's' : 
            send += node_event.len
        elif node_event.event_type == 'r' :
            recv += node_event.len
        elif node_event.event_type == 'f' :
            fwd += node_event.len
        elif node_event.event_type == 'd' :
            drp += node_event.len
    csv.close()

    
    


